﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;

using System.Data.OleDb;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Security;
using System.Security.Cryptography;
using FastReport;


namespace Business
{
    public partial class chubiao : Form
    {
        
        public bool ckbgd = true;
        public string syjd = "";
        public string slsh = "";
        public string jclb = "";
        public string shpzl = "";
        public string shphm = "";
        public string scllx = "";
        public string szs = "";
        public string sjyjl = "";
        public string sdyzt = "";
        public DataTable dt = new DataTable();
        public string jyxm = "";
        public string dlyszh = "";
        public string Url = System.Windows.Forms.Application.StartupPath + "\\安检.frx";
        Report frt = new Report();
        Class_DES des = new Class_DES();

        public string sjcbz = "21861";
        public 报告单.新车 BAOBIAO = new 报告单.新车();
        public bool  bbhgedy=false;
        public bool bptcsyz = false;
        public bool yjgn = false;
        public chubiao()
        {
            InitializeComponent();
            getlwcj();
        }
      
        /// <summary>
        /// 打印控件事件开始准备工作
        /// 获取打印模板文件
        /// </summary>
        public void getlwcj()
        {
            Query mquery = new Query();
            string ssql = string.Format(@"Select * From StationInfor ");
            sjcbz = mquery.GetDBString(ssql, "Standard");
            bbhgedy = mquery.GetDBString(ssql, "SI_bhgdy").Contains("True") ? true : false;
            bptcsyz = mquery.GetDBString(ssql, "SI_bptcsyz").Contains("True") ? true : false;//平台次数一致
        }

        private float X;
        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize <= 0)
                {
                    currentSize = 0.1f;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        string str;
        private void chubiao_Load(object sender, EventArgs e)
        {
            Query mquery = new Query();

            X = this.Width;
            Y = this.Height;


            setTag(this);

            INIfile iniex = new INIfile();
            iniex.path = Application.StartupPath + @"\Business.ini";
            string sip = m_mainform.DecodeBase64("utf-8", iniex.IniReadValue("设置", "数据库IP"));
            string sdatebase = m_mainform.DecodeBase64("utf-8",iniex.IniReadValue("设置", "数据库名称"));
            string suser = m_mainform.DecodeBase64("utf-8",iniex.IniReadValue("设置", "数据库用户名"));
            string spwd = m_mainform.DecodeBase64("utf-8",iniex.IniReadValue("设置", "数据库密码"));
            str = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sip, sdatebase, suser, spwd);

            //判断是否民警
            if (m_mainform.sfjy.Contains("检测数据查询"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_cxjl.Enabled = false;
                }
            }

            //判断是否民警
            if (m_mainform.sfjy.Contains("车辆出表"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    预览报告单ToolStripMenuItem.Enabled = false;
                }
            }

        }
        public MainForm m_mainform;
        private void but_cxjl_Click(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("检测数据查询"))
            {
                m_mainform.baocunhxrj("检测数据查询", string.Format("检测数据查询"),"查询");// 核心功能日记  日记内容
            }
            getjiluAJ();
        }

        public void getjiluAJ()
        {
            lv.Items.Clear();
            Query mquery = new Query();

            string sql = string.Format(@"SELECT AT_HPHM,AT_HPZL,AT_CLXH,DETECT_ID,DET_JYCS,DET_JCLB,DET_DATE,DET_JYZJL,DET_LSH,DET_Location,DET_DYZT,DET_FINISH from AUTO,Detects_SJA where AUTO.AT_ID=Detects_SJA.AT_ID");
            if (text_shphm.Text != "")
            {
                sql = sql + string.Format(@" and dbo.AUTO.AT_HPHM like '%{0}%'", text_shphm.Text);
            }
            if (text_clxh.Text != "")
            {
                sql = sql + string.Format(@" and dbo.AUTO.AT_CLXH like '%{0}%'", text_clxh.Text);
            }
            if (che_date.Checked)
            {
                sql = sql + string.Format(@" and Detects_SJA.DET_DATE>'{0}' and Detects_SJA.DET_DATE<'{1}'", date_kaishi.Value.ToShortDateString() + " 00:00:00", date_jieshu.Value.ToShortDateString() + " 23:59:59");
            }

            sql = sql + " order by Detects_SJA.DETECT_ID desc ";

            DataTable dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                     string jycs =dt.Rows[i]["DET_JYCS"].ToString().Trim();
                    ListViewItem lt = new ListViewItem(dt.Rows[i]["DETECT_ID"].ToString().Trim());


                    lt.SubItems.Add(dt.Rows[i]["AT_HPHM"].ToString().Trim());

                    lt.SubItems.Add(dt.Rows[i]["AT_CLXH"].ToString().Trim());

                    lt.SubItems.Add(string.Format("{0}{1}", dt.Rows[i]["DET_JCLB"].ToString().Trim(), string.Format(@"({0})", jycs)));
                    lt.SubItems.Add(dt.Rows[i]["DET_DATE"].ToString().Trim());
                    
                    string jl = dt.Rows[i]["DET_JYZJL"].ToString().Trim();

                    if (jl.Contains("0"))
                    {
                        lt.SubItems.Add("");
                    }
                    else if (jl.Contains("1"))
                    {
                        lt.SubItems.Add("合格");
                    }
                    else if (jl.Contains("2"))
                    {
                        lt.SubItems.Add("不合格");
                    }
                       
                    if (dt.Rows[i]["DET_Location"].ToString().Trim().Length < 1)
                    {
                        lt.SubItems.Add("未检");
                    }
                    else
                    {
                        lt.SubItems.Add(dt.Rows[i]["DET_Location"].ToString().Trim());

                    }
                    if (string.IsNullOrEmpty(dt.Rows[i]["DET_DYZT"].ToString().Trim()))
                    {
                        lt.SubItems.Add("未打印");
                    }
                    else
                    {
                        lt.SubItems.Add(dt.Rows[i]["DET_DYZT"].ToString().Trim());
                    }

                    lt.SubItems.Add("");
                    this.lv.Items.Add(lt);


                }

            }
            else
            {
                MessageBox.Show("没有记录集");
            }
        }

        private void 预览报告单ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            int ilsh = 0;
            int.TryParse(slsh.Trim(), out ilsh);
            if (ilsh < 1)
            {
                MessageBox.Show("请选择需要操作的流水记录");
                return;
            }
            Url = System.Windows.Forms.Application.StartupPath + "\\新车报告.frx";
            BAOBIAO = new 报告单.新车();
            BAOBIAO.bgd(slsh, Url, "2");
        }

        public void deletejilu(int ilsh)
        {
            Query mquery = new Query();
            string ssql = string.Format(@"delete Detects_SJA where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete Detects_SJB where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete Detects_SJC where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete Detects_SJD where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete PD_AJ where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete PD_ZJ where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete  ZBZLPhoto where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete LWHQXM where DETECT_ID={0}", ilsh);
            mquery.InsertDB(ssql);
            ssql = string.Format(@"delete WJXM where WGXM_jylsh in (select DET_LSHZJ from Detects_SJA where DETECT_ID='{0}')", ilsh);
            mquery.InsertDB(ssql);

        }

        private void 打印报告单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int ilsh = 0;
            int.TryParse(slsh.Trim(), out ilsh);
            if (ilsh < 1)
            {
                MessageBox.Show("请选择需要操作的流水记录");
                return;
            }
            Url = System.Windows.Forms.Application.StartupPath + "\\新车报告.frx";
            BAOBIAO = new 报告单.新车();
            BAOBIAO.bgd(slsh, Url, "1");

            Query mquery = new Query();
            string sql = string.Format(@"update Detects_SJA set DET_DYZT ='已打印'where DETECT_ID='{0}'", ilsh);
            mquery.InsertDB(sql);

        }

         private void 查看检验项目ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             Query q = new Query();
             string sql = string.Format("select * from Detects_SJA where DETECT_ID={0}", slsh);
             DataTable DT = q.GetTable(sql);
             if (DT.Rows.Count > 0)
             {
                 string xm = DT.Rows[0]["DET_BJYXM"].ToString().Trim();
                 jcxm m_jcxm = new jcxm(xm, "1", false, slsh);
                 m_jcxm.ShowDialog();
             }

         }

         private void 删除车辆ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             if (MessageBox.Show("是否删除该流水号:" + slsh, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
             {
                 Query mquery = new Query();
                 string ssql = string.Format(@"delete Detects_SJA where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 ssql = string.Format(@"delete Detects_SJB where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 ssql = string.Format(@"delete Detects_SJC where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 ssql = string.Format(@"delete Detects_SJD where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 ssql = string.Format(@"delete PD_AJ where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 ssql = string.Format(@"delete LWHQXM where DETECT_ID='{0}'", slsh);
                 mquery.InsertDB(ssql);
                 MessageBox.Show("删除成功");
                 getjiluAJ();
             }
             

         }

         private void lv_MouseUp(object sender, MouseEventArgs e)
         {
             Query mqury = new Query();
             shpzl = "";
             slsh = "";
             jclb = "";
             syjd = "";
             text_shphm.Text = "";
             shphm="";
             scllx = "";
             szs = "";
             foreach (ListViewItem item in lv.SelectedItems)
             {
                 shpzl = item.SubItems[2].Text.ToString().Trim();              
                 text_shphm.Text = item.SubItems[1].Text.ToString().Trim();  
                 shphm = item.SubItems[1].Text.ToString().Trim();
                 slsh = item.SubItems[0].Text.ToString().Trim();
                 jclb = item.SubItems[3].Text.ToString().Trim();
                 sjyjl = item.SubItems[5].Text.ToString().Trim();
                 sdyzt = item.SubItems[7].Text.ToString().Trim();
             }
             string sql = string.Format(@"select AUTO.AT_CLLX,AUTO.AT_ZS,AUTO.AT_DLYSZ FROM AUTO,Detects_SJA where AUTO.AT_ID=Detects_SJA.AT_ID and Detects_SJA.DETECT_ID='{0}'", slsh);
             scllx = mqury.GetDBString(sql, "AT_CLLX");
             szs = mqury.GetDBString(sql, "AT_ZS");
             dlyszh = mqury.GetDBString(sql, "AT_DLYSZ");

         }

         private void 不合格复检ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             string lb = "1";
             m_mainform.denglu(shphm, shpzl,lb);
             
         }

         private void panelControl1_DoubleClick(object sender, EventArgs e)
         {
             if (panelControl1.Dock == DockStyle.Right)
             {
                 lv.Dock = DockStyle.None;
                 panelControl1.Dock = DockStyle.Left;
                 lv.Dock = DockStyle.Fill;
             }
             else
             {
                 lv.Dock = DockStyle.None;
                 panelControl1.Dock = DockStyle.Right;
                 lv.Dock = DockStyle.Fill;
             }
         }

        private void 设计报告单ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            int ilsh = 0;
            int.TryParse(slsh.Trim(), out ilsh);
            if (ilsh < 1)
            {
                MessageBox.Show("请选择需要操作的流水记录");
                return;
            }
            Url = System.Windows.Forms.Application.StartupPath + "\\新车报告.frx";
            BAOBIAO = new 报告单.新车();
            BAOBIAO.bgd(slsh, Url, "0");
        }
    }
}
